#!/usr/bin/env python
# encoding: utf-8
"""
test.py

Created by Roman on 2010-01-17.
Copyright (c) 2010 __MyCompanyName__. All rights reserved.
"""

import sys
import os

import urllib2 as u2
import httplib

    

def unshortUrl(shortUrl):
    class RedirectHandler(u2.HTTPRedirectHandler):
        def __init__(self, reporter):
            self.reporter = reporter 

        def redirect_request(self, req, fp, code, msg, hdrs, newurl):
            self.reporter.setUrl(newurl)
            raise u2.HTTPError(newurl, code, msg, hdrs, fp)

    class Reporter:
        def setUrl(self, url):
            self.url = url

    r = Reporter()
    opener = u2.build_opener(RedirectHandler(r))
    
    try:
        opener.open(shortUrl)
    except:
        return r.url


print unshortUrl('http://twitpic.com/yhnh3')
        
